import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie'

Vue.use(VueRouter)

const routes = [
  // 首页
  {
    path: '/',
    name: '',
    meta: { requiresAuth: true }, // 添加一个meta字段以标识需要身份验证的路由
    component: () => import('../views/HomeView.vue'),
    redirect: '/home',
    children: [
      {
        path: '',
        name: 'home',
        meta: { requiresAuth: true },
        component: () => import('../components/home/home.vue')

      },
      {
        path: 'artworkInformation',
        name: 'artworkInformation',
        meta: { requiresAuth: true },
        component: () => import('../components/home/artworkInformation.vue')

      },
      {
        path: 'artistInformation',
        name: 'artistInformation',
        meta: { requiresAuth: true },
        component: () => import('../components/home/artistInformation.vue')

      },
      {
        path: 'shelves',
        name: 'shelves',
        meta: { requiresAuth: true },
        component: () => import('../components/home/shelves.vue')

      },
      {
        path: 'shoppingCart',
        name: 'shoppingCart',
        meta: { requiresAuth: true },
        component: () => import('../components/home/shoppingCart.vue')

      },
      {
        path: 'myOrders',
        name: 'myOrders',
        meta: { requiresAuth: true },
        component: () => import('../components/home/myOrders.vue')

      },
      {
        path: 'personalInformation',
        name: 'personalInformation',
        meta: { requiresAuth: true },
        component: () => import('../components/home/personalInformation.vue')

      },
    ]
  },
   // 商品详情页
   {
    path: '/ItemDetails',
    name: 'ItemDetails',
    component: () => import('../views/ItemDetails.vue')
  },
   // 艺术家详情页
   {
    path: '/ArtistDetails',
    name: 'ArtistDetails',
    component: () => import('../views/ArtistDetails.vue')
  },
 
  // 登录注册
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },
    // 数据可视化大屏
    {
      path: '/Visualization',
      name: 'Visualization',
      meta: { requiresAuth: true }, // 添加一个meta字段以标识需要身份验证的路由
      component: () => import('../views/Visualization/index.vue'),
      // redirect: '/index',
      children: [
        {
          path: 'index',
          name: 'index',
          meta: { requiresAuth: true },
          component: () => import('../views/Visualization/index.vue')
        },
        {
          path: 'UserRoles',
          name: 'UserRoles',
          meta: { requiresAuth: true },
          component: () => import('../views/Visualization/UserRoles.vue')
        },
        {
          path: 'PurchaseType',
          name: 'PurchaseType',
          meta: { requiresAuth: true },
          component: () => import('../views/Visualization/PurchaseType.vue')
        },
        {
          path: 'ArtistWork',
          name: 'ArtistWork',
          meta: { requiresAuth: true },
          component: () => import('../views/Visualization/ArtistWork.vue')
        },
        {
          path: 'DataOverview',
          name: 'DataOverview',
          meta: { requiresAuth: true },
          component: () => import('../views/Visualization/DataOverview.vue')
        },
        {
          path: 'comments',
          name: 'comments',
          meta: { requiresAuth: true },
          component: () => import('../views/Visualization/Comments.vue')
        },
        {
          path: 'UploadWork',
          name: 'UploadWork',
          meta: { requiresAuth: true },
          component: () => import('../views/Visualization/UploadWork.vue')
        },
      ]
    },
  // 管理员后台
  {
    path: '/admin',
    name: 'admin',
    meta: { requiresAuth: true }, // 添加一个meta字段以标识需要身份验证的路由
    component: () => import('../views/AdminView.vue'),
    redirect: '/PersonnelManagement',
    children: [
      {
        path: 'PersonnelManagement',
        name: 'PersonnelManagement',
        meta: { requiresAuth: true },
        component: () => import('../components/admin/PersonnelManagement.vue')
      },
      {
        path: 'OrderManagement',
        name: 'OrderManagement',
        meta: { requiresAuth: true },
        component: () => import('../components/admin/OrderManagement.vue')
      },
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!isLoggedIn()) {
      next('/login');
    } else {
      next();
    }
  } else {
    next();
  }
});

// 检查用户是否已登录
function isLoggedIn() {
  let login = Cookies.get('token')
  if (login != undefined) {
    return true;
  } else {
    return false;
  }
}

export default router
